package org.wanho;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Author：汤小洋
 * Date：2023-05-09 10:39
 * Description：<描述>
 */
public class Test11_终止操作 {

    public static void main(String[] args) {
        test6();
    }

    /**
     * 查找与匹配
     */
    public static void test1(){
        // 是否年龄大于25的员工都在休假？
        boolean allMatch = Emp.getTestData().stream()
                .filter(e -> e.getAge() > 25)
                .allMatch(e -> {
                    System.out.println(e);
                    return e.getState().equals(State.VOCATION);
                });
        System.out.println(allMatch);
        System.out.println("-------------------------------------------");

        // 是否有员工处于忙碌状态
        boolean anyMatch = Emp.getTestData().stream().anyMatch(e -> {
            System.out.println(e);
            return e.getState().equals(State.BUSY);
        });
        System.out.println(anyMatch);
        System.out.println("-------------------------------------------");

        // 是否所有员工的年龄都不小于18
        boolean noneMatch = Emp.getTestData().stream().noneMatch(e -> {
            System.out.println(e);
            return e.getAge() < 18;
        });
        System.out.println(noneMatch);
        System.out.println("-------------------------------------------");

        // 获取一个工资大于2000的员工
        Optional<Emp> opt = Emp.getTestData().stream()
                .filter(e -> e.getSalary() > 2000)
                // .findFirst();
                .findAny();
        if (opt.isPresent()){
            Emp emp = opt.get();
            System.out.println(emp);
        }else{
            System.out.println("不存在工资大于2000的员工");
        }
        System.out.println("-------------------------------------------");

        // 统计有多少人休假
        long count = Emp.getTestData().stream()
                .filter(e -> e.getState().equals(State.VOCATION))
                .count();
        System.out.println(count);
        System.out.println("-------------------------------------------");

        // 找出工资最高的员工
        opt = Emp.getTestData().stream()
                .max((e1, e2) -> (int) (e1.getSalary() - e2.getSalary()));
        System.out.println(opt);
    }

    /**
     * 归约
     */
    public static void test2(){
        // 计算数组中元素之和
        Integer[] nums = {4,8,5};
        Optional<Integer> opt = Arrays.stream(nums).reduce((t, c) -> {
            // t是上一次的累计结果
            // c是本次的元素
            System.out.println(t + "----" + c);
            return t + c;
        });
        System.out.println(opt);
        // System.out.println(Arrays.stream(nums).reduce((t,c)->t+c));

        int value = 1000;
        System.out.println(Arrays.stream(nums).reduce(value,(t,c)->t+c));

    }

    /**
     * 收集
     */
    public static void test3(){
        // 获取所有员工的姓名
        List<String> list = Emp.getTestData().stream()
                .map(Emp::getName)
                .collect(Collectors.toList());// 使用List集合
        System.out.println(list);

        Set<String> set = Emp.getTestData().stream()
                .map(Emp::getName)
                .collect(Collectors.toSet());// 使用List集合
        System.out.println(set);
    }

    /**
     * 统计
     */
    public static void test4(){
        // 休假的总人数
        Long count = Emp.getTestData().stream()
                .filter(e -> e.getState().equals(State.VOCATION))
                .collect(Collectors.counting());
        System.out.println(count);
        System.out.println("-------------------------------------");

        Double sum = Emp.getTestData().stream()
                .collect(Collectors.summingDouble(Emp::getSalary));
        System.out.println(sum);
        System.out.println("-------------------------------------");

        // Collectors.averagingDouble()

        Optional<Emp> opt = Emp.getTestData().stream()
                .collect(Collectors.maxBy((e1, e2) -> (int) (e1.getSalary() - e2.getSalary())));
        System.out.println(opt);
    }

    /**
     * 拼接
     */
    public static void test5(){
        String str = Emp.getTestData().stream()
                .map(Emp::getName)
                // .collect(Collectors.joining("-"));
                .collect(Collectors.joining(",","{","}"));
        System.out.println(str);
    }

    /**
     * 分组
     */
    public static void test6(){
        Map<State, List<Emp>> map = Emp.getTestData().stream()
                .collect(Collectors.groupingBy(e -> e.getState()));
        // 遍历
        for(State key : map.keySet()){
            System.out.println(key);
            // map.get(key).forEach(e -> System.out.println(e));
            map.get(key).forEach(System.out::println);
        }
    }

}
